package com.taglab.chic.atom;

import com.taglab.chic.atom.Category;
import com.taglab.chic.atom.Link;
import com.taglab.chic.atom.Person;
import com.taglab.chic.atom.Text;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.io.Serializable;

/**
 * @author Markus Kobler
 */
public abstract class BaseAtomElement implements Serializable {
  
  private String id;
  private String title;
  protected Date updated = new Date();

  // optional
  private List<Person> authors;
  private List<Person> contributors;
  private List<Category> categories;
  private List<Link> links;

  private Text rights;

  protected BaseAtomElement() {}

  protected BaseAtomElement(String id, String title) {
    if( id == null ) throw new IllegalArgumentException( "id cannot be null");
    if( title == null ) throw new IllegalArgumentException( "title cannot be null");
    setId(id);
    this.title = title;
  }

  public String getId() {
    return id;
  }

  protected void setId(String id) {
    this.id = id;
    // XXX check to see if looks like url in which case add an rel="alternative" link
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public Date getUpdated() {
    return updated;
  }

  public void setUpdated(Date updated) {
    this.updated = updated;
  }

  public List<Person> getAuthors() {
    return authors == null ? null : Collections.unmodifiableList(authors);
  }
  
  public void addAuthor(Person author) {
    if( authors == null ) authors = new ArrayList<Person>();
    authors.add(author);
  }

  public List<Person> getContributors() {
    return contributors == null ? null : Collections.unmodifiableList(contributors);
  }

  public void addContributor(Person contributor) {
    if( contributors == null ) contributors = new ArrayList<Person>();
    contributors.add(contributor);
  }

  public List<Category> getCategories() {
    return categories == null ? null : Collections.unmodifiableList(categories);
  }

  public void addCategory(Category category) {
    if( categories == null ) categories = new ArrayList<Category>();
    categories.add(category);
  }

  public List<Link> getLinks() {
    return links == null ? null : Collections.unmodifiableList(links);
  }

  public void addLink(Link link) {
    if( links == null ) links = new ArrayList<Link>();
    this.links.add(link);
  }

  public Text getRights() {
    return rights;
  }

  public void setRights(Text rights) {
    this.rights = rights;
  }

  public void setRights(String rights) {
    this.rights =  rights != null ? new Text(rights) : null;
  }

}
